﻿namespace QFlow.Windows.Forms
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using QFlow.Data;


    /// <summary>
    /// Products List Form
    /// </summary>
    public partial class ProductsList : QFlow.Utils.BaseForm
    {
        private ProductType _ResultType;

        public ProductsList(ProductType resultType, QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            this._ResultType = resultType;
            if (_ResultType != ProductType.Stockable)
            {
                clmName.DataPropertyName = "Name";
                clmUnit.Visible = false;
                clmBarcode.Visible = false;
                clmQuantity.Visible = false;
                clmSalePrice.Visible = false;
                clmPurchasePrice.Visible = false;
            }
        }

        private void FillBrands()
        {
            this.cmbBrands.ComboBoxEx.DisplayMember = "Name";
            this.cmbBrands.ComboBoxEx.ValueMember = "BrandID";
            this.cmbBrands.ComboBoxEx.DataSource = this.Context.ProductBrands.ToArray();

            this.cmbBrands.SelectedIndex = -1;
        }

        private void FillCategories()
        {
            this.cmbCategories.ComboBoxEx.DisplayMember = "Name";
            this.cmbCategories.ComboBoxEx.ValueMember = "CategoryID";
            this.cmbCategories.ComboBoxEx.DataSource = this.Context.ProductCategories.ToArray();

            this.cmbCategories.SelectedIndex = -1;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            RefereshGrid();
        }

        private void ProductsList_Load(object sender, EventArgs e)
        {
            this.FillBrands();
            this.FillCategories();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewProduct product = new NewProduct(_ResultType, true, this.Context);
            product.MdiParent = this.MdiParent;
            product.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                int productId = 0;
                if (_ResultType == ProductType.Stockable)
                    productId = (gvResults.SelectedRows[0].DataBoundItem as StockableProduct).ProductID;
                else productId = (gvResults.SelectedRows[0].DataBoundItem as Product).ProductID;
                NewProduct product = new NewProduct(_ResultType, productId, this.Context);
                product.MdiParent = this.MdiParent;
                product.Show();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                if (MessageBox.Show(QFlow.Windows.Properties.Resources.MBDeleteProduct, QFlow.Windows.Properties.Resources.MBConfirmCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    try
                    {
                        if (_ResultType == ProductType.Stockable)
                        {
                            StockableProduct product = gvResults.SelectedRows[0].DataBoundItem as StockableProduct;
                            this.Context.StockableProducts.DeleteOnSubmit(product);
                            this.Context.SubmitChanges();
                        }
                        else
                        {
                            Product product = gvResults.SelectedRows[0].DataBoundItem as Product;
                            this.Context.Products.DeleteOnSubmit(product);
                            this.Context.SubmitChanges();
                        }
                        RefereshGrid();
                    }
                    catch
                    {
                        MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantDeleteProduct, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    }
                }
            }
        }

        private void RefereshGrid()
        {
            if (_ResultType == ProductType.Stockable)
            {
                IEnumerable<StockableProduct> lst = this.Context.StockableProducts.ToArray();

                if (this.cmbCategories.ComboBoxEx.SelectedIndex != -1)
                    lst = lst.Where(p => p.Product.CategoryID == (int)this.cmbCategories.ComboBoxEx.SelectedValue);

                if (this.cmbBrands.ComboBoxEx.SelectedIndex != -1)
                    lst = lst.Where(p => p.Product.BrandID == (int)this.cmbBrands.ComboBoxEx.SelectedValue);

                if (!string.IsNullOrEmpty(this.txtID.TextBox.Text))
                    lst = lst.Where(p => p.ProductID.ToString() == this.txtID.TextBox.Text);

                if (!string.IsNullOrEmpty(this.txtName.TextBox.Text))
                    lst = lst.Where(p => p.Product.Name.Contains(this.txtName.TextBox.Text));

                gvResults.AutoGenerateColumns = false;
                gvResults.DataSource = lst.ToArray();
            }
            else
            {
                IEnumerable<Product> lst = null;
                if (_ResultType == ProductType.NonStockable)
                    lst = this.Context.Products.Where(product => product.IsNonStockable && !product.IsService);
                else lst = this.Context.Products.Where(product => product.IsService);

                if (this.cmbCategories.ComboBoxEx.SelectedIndex != -1)
                    lst = lst.Where(p => p.CategoryID == (int)this.cmbCategories.ComboBoxEx.SelectedValue);

                if (this.cmbBrands.ComboBoxEx.SelectedIndex != -1)
                    lst = lst.Where(p => p.BrandID == (int)this.cmbBrands.ComboBoxEx.SelectedValue);

                if (!string.IsNullOrEmpty(this.txtID.TextBox.Text))
                    lst = lst.Where(p => p.ProductID.ToString() == this.txtID.TextBox.Text);

                if (!string.IsNullOrEmpty(this.txtName.TextBox.Text))
                    lst = lst.Where(p => p.Name.Contains(this.txtName.TextBox.Text));

                gvResults.AutoGenerateColumns = false;
                gvResults.DataSource = lst.ToArray();
            }
        }

        private void ProductsList_Activated(object sender, EventArgs e)
        {
            RefereshGrid();
        }

        private void gvResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                int productId = 0;
                if (_ResultType == ProductType.Stockable)
                    productId = (gvResults.SelectedRows[0].DataBoundItem as StockableProduct).ProductID;
                else productId = (gvResults.SelectedRows[0].DataBoundItem as Product).ProductID;
                NewProduct product = new NewProduct(_ResultType, productId, this.Context);
                product.MdiParent = this.MdiParent;
                product.Show();
            }
        }

        private void gvResults_SelectionChanged(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
            }
            else
            {
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
            }
        }
    }
}